package com.lishe.lishe.lishe.module.mall.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.lishe.lishe.Imageloader.FlexibleRoundeBitmapDisplayer;
import com.lishe.lishe.Imageloader.ImageLoaderUtils;
import com.lishe.lishe.R;
import com.lishe.lishe.lishe.MyRecyclerView.RecyclerviewBaseAdapter;
import com.lishe.lishe.lishe.module.mall.bean.MallItemObject;
import com.lishe.lishe.lishe.module.mall.presenter.MallMainAdapterPresenter;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

import java.util.List;

/**
 * Created by shino on 2017/11/6.
 * 超值专区
 */

public class MallValuseAdapter extends RecyclerviewBaseAdapter {
    List<MallItemObject> mallItemObjects;
    Context context;
    ImageLoaderUtils imageLoaderUtils;

    public MallValuseAdapter(Context context, List<MallItemObject> mallItemObjects) {
        this.mallItemObjects = mallItemObjects;
        this.context = context;
        DisplayImageOptions options0 = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.lishe_png) // 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.mipmap.lishe_png) // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.mipmap.lishe_png) // 设置图片加载或解码过程中发生错误显示的图片
                .bitmapConfig(Bitmap.Config.ARGB_8888)   //设置图片的解码类型
                .cacheOnDisk(true)
                .cacheInMemory(true)
                .build();
        imageLoaderUtils = new ImageLoaderUtils(options0,0);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolderMy(ViewGroup parent, int viewType) {
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_mall_valuse_image, null);
        view.setLayoutParams(params);
        Holder holder = new Holder(view);
        return holder;
    }

    @Override
    public void onBindViewHolderMy(RecyclerView.ViewHolder holder0, int position) {
        Holder holder = (Holder) holder0;
        final MallItemObject mallItemObject = mallItemObjects.get(position);
        imageLoaderUtils.displayImage(mallItemObject.getImg_url(), holder.image);
        holder.image.setAdjustViewBounds(true);
        holder.image.setScaleType(ImageView.ScaleType.FIT_XY);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MallMainAdapterPresenter.valuseItemOnClick((Activity) context, mallItemObject);
            }
        });
    }

    @Override
    public int getItemCountMy() {
        return mallItemObjects == null ? 0 : mallItemObjects.size();
    }

    @Override
    public int getItemViewTypeMy(int position) {
        return 0;
    }

    class Holder extends RecyclerView.ViewHolder {
        ImageView image;

        public Holder(View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.image);
        }
    }
}
